﻿using System;
using System.Windows;
using MyFloridaHouse.libSvOpenDistricts.StorageIsolated;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;

namespace MyFloridaHouse.libSvOpenDistricts.DataService
{
    public delegate void ServiceDALGetDistrictPlanTypeCompletedEventHandler(DistrictPlanTypeSTO sto);

    public partial class ServiceDAL
    {
        public static event ServiceDALGetDistrictPlanTypeCompletedEventHandler GetDistrictPlanTypeCompleted;

        // usage(called once at setup):
        public void GetDistrictPlanTypes()
        {
            try
            {
				var proxy = new DistrBldrClient(_relativeBindingName);
                proxy.StartupGetDistrictPlanTypesCompleted += OnServiceGetDistrictPlanTypesCompleted;
                proxy.StartupGetDistrictPlanTypesAsync(IsolatedStorageHelper.GetDistrictPlanTypesFileDate());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void OnServiceGetDistrictPlanTypesCompleted(object sender, StartupGetDistrictPlanTypesCompletedEventArgs args)
        {
            if (!IsError("GetDistrictPlanTypesCompleted", args.Error, args.Result, args.Result.errorMessage))
            {
                RaiseGetDistrictPlanTypeCompleted(this, args.Result);
            }
        }

        private static void RaiseGetDistrictPlanTypeCompleted(object sender, DistrictPlanTypeSTO sto)
        {
            if (GetDistrictPlanTypeCompleted != null)
            {
                GetDistrictPlanTypeCompleted(sto);
            }
            else
            {
                // todo: Log this error
                throw (new NotImplementedException("No Subscribers to ServiceDALGetDistrictPlanTypeCompleted Event"));
            }
        }
	}
}
